import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-03-13
 * Time: 21:27
 */
public class PushPopMatch {
    Stack<Integer> stack = new Stack<>();
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        int j =0;
        for (int i = 0; i < pushV.length; i++) {
            //1.压入栈中
            stack.push(pushV[i]);
            //2.将栈顶元素与popV数组比较，相同则弹出并且j++，反之继续压栈。
            //当这个条件stack.peek() == popV[j]出来了，你需要确保栈不能为空并且数组不能越界。
            while(j<popV.length &&!stack.empty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    public static void main(String[] args) {
        int[] pushA = {1,2,3,4,5};
        int[] popA = {4,5,3,2,1};
        PushPopMatch pushPopMatch = new PushPopMatch();
        System.out.println(pushPopMatch.IsPopOrder(pushA, popA));
    }
}
